<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { Lock, User, UserFilled, Tools, Avatar, Key, Check } from '@element-plus/icons-vue'

// 引入路由和用户状态管理
const router = useRouter()
const userStore = useUserStore()

// 表单数据
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  role: 'user' // 默认为普通用户
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  role: [{ required: true, message: '请选择用户角色', trigger: 'change' }]
}

// 表单引用
const formRef = ref()

// 角色选项
const roleOptions = [
  {
    value: 'user',
    label: '普通用户',
    icon: User,
    description: '可以查看道路养护信息和上报道路损坏'
  },
  {
    value: 'maintainer',
    label: '养护人员',
    icon: Tools,
    description: '负责道路养护工作和处理损坏报告'
  }
]

// 注册处理
const handleRegister = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      const success = userStore.register(
        registerForm.username,
        registerForm.password,
        registerForm.role
      )

      if (success) {
        ElMessage.success('注册成功，已自动登录')
        if (registerForm.role === 'maintainer') {
          router.push('/maintenance')
        } else {
          router.push('/')
        }
      } else {
        ElMessage.error('注册失败，用户名已存在')
      }
    } else {
      return false
    }
  })
}

// 返回登录页
const goToLogin = () => {
  router.push('/login')
}

// 返回欢迎页
const goToWelcome = () => {
  router.push('/welcome')
}
</script>

<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <img src="../assets/logo.svg" alt="Logo" class="logo" />
        <h2>道路养护管理系统</h2>
        <p>用户注册</p>
        <div class="register-subtitle">请填写以下信息完成注册</div>
      </div>

      <el-form
        ref="formRef"
        :model="registerForm"
        :rules="rules"
        class="register-form"
        label-position="top"
      >
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <el-icon><Avatar /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password" label="密码">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Key /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="confirmPassword" label="确认密码">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="role" label="用户角色">
          <div class="role-options">
            <div
              v-for="item in roleOptions"
              :key="item.value"
              class="role-option"
              :class="{ 'role-selected': registerForm.role === item.value }"
              @click="registerForm.role = item.value"
            >
              <div class="role-icon">
                <el-icon :size="24"><component :is="item.icon" /></el-icon>
              </div>
              <div class="role-info">
                <div class="role-name">{{ item.label }}</div>
                <div class="role-description">{{ item.description }}</div>
              </div>
              <div class="role-check" v-if="registerForm.role === item.value">
                <el-icon><Check /></el-icon>
              </div>
            </div>
          </div>
        </el-form-item>

        <div class="register-tips">
          <p>注册成功后将自动登录系统</p>
        </div>

        <el-form-item>
          <el-button type="primary" class="register-button" @click="handleRegister">注册</el-button>
        </el-form-item>

        <div class="form-footer">
          <span>已有账号？</span>
          <el-button type="text" @click="goToLogin">立即登录</el-button>
          <el-divider direction="vertical" />
          <el-button type="text" @click="goToWelcome">返回首页</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #e6f7ff 0%, #d0e8f2 100%);
  background-size: cover;
}

.register-box {
  width: 450px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 15px;
}

.register-header h2 {
  font-size: 24px;
  color: #409EFF;
  margin: 0;
  margin-bottom: 10px;
}

.register-header p {
  font-size: 16px;
  color: #606266;
  margin: 0;
}

.register-form {
  margin-top: 20px;
}

.register-form .el-form-item {
  margin-bottom: 20px;
}

.register-subtitle {
  font-size: 14px;
  color: #909399;
  margin-top: 10px;
}

.role-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}

.role-option {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.role-option:hover {
  border-color: #409eff;
  background-color: #f0f9ff;
}

.role-selected {
  border-color: #409eff;
  background-color: #f0f9ff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.role-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background-color: #409eff;
  color: white;
  border-radius: 8px;
  margin-right: 15px;
}

.role-info {
  flex: 1;
}

.role-name {
  font-weight: bold;
  font-size: 16px;
  color: #303133;
  margin-bottom: 5px;
}

.role-description {
  font-size: 12px;
  color: #909399;
}

.role-check {
  color: #409eff;
  font-size: 18px;
}

.register-button {
  width: 100%;
  padding: 12px 0;
  margin-top: 10px;
  background-color: #409EFF;
}

.register-tips {
  font-size: 12px;
  color: #909399;
  margin-bottom: 15px;
  background-color: #f8f8f8;
  padding: 8px;
  border-radius: 4px;
}

.register-tips p {
  margin: 5px 0;
}

.form-footer {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #606266;
}
</style>
